package vehement;

/*
 * Copyright (c) 2002-2008 LWJGL Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'LWJGL' nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * 3-D gear wheels. Originally by Brian Paul
 */
//package org.lwjgl.test.opengl;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.ARBTransposeMatrix;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.util.glu.Project;

import controller.Controller;

import vehement.game.GameThread;
import vehement.game.Globals;

import vehement.game.graphics.Ob;
/**
 * <p>
 * This is the OpenGL "standard" Gears demo, originally by Brian Paul
 * </p>
 * @author Brian Matzon <brian@matzon.dk>
 * @version $Revision$
 * $Id$
 */
public class VMain {

	private int screenWidth = 1024;
	private int screenHeight = 800;

    public static GameThread game;
    public static boolean paused;
    private Ob glPrimitives;

    
	public static void main(String[] args) {
		new VMain().execute();
		System.exit(0);
	}

	/**
	 * 
	 */
	private void execute() {
		try {
			init();
		} catch (LWJGLException le) {
			le.printStackTrace();
			System.out.println("Failed to initialize Gears.");
			return;
		}
        game = new GameThread();
        new Thread(game).start();
        paused = false;        
        // We are now set up, lets loop
        loop();
		
		//game done, lets get rid of some stuff
		destroy();
	}

	/**
	 * This keeps track of everything that needs to be destroyed.
	 */
	private void destroy() {
		//we should also stop the game thread here.
		game.gameOver();
		Keyboard.destroy();
		Display.destroy();
	}

	/**
	 * 
	 * This is the display loop. This draws the game and keeps track of fps
	 */
	private void loop() {
		long startTime = System.currentTimeMillis() + 5000;
		long fps = 0;

		while (!Display.isCloseRequested()) {
		
			game.draw();
			
			if (startTime > System.currentTimeMillis()) {
				fps++;
			} else {
				long timeUsed = 5000 + (startTime - System.currentTimeMillis());
				startTime = System.currentTimeMillis() + 5000;
				System.out.println(fps + " frames in " + (float) (timeUsed / 1000f) + " seconds = "
						+ (fps / (timeUsed / 1000f)));
				fps = 0;
			}
		}
	}

	/**
	 * 
	 */
	private void init() throws LWJGLException {
		// create Window of size 300x300
		Display.setLocation((Display.getDisplayMode().getWidth() - screenWidth) / 2,
												(Display.getDisplayMode().getHeight() - screenHeight) / 2);
		Display.setDisplayMode(new DisplayMode(screenWidth, screenHeight));
		Display.setTitle("Vehement");
		Display.sync(60);
		Display.create();

		// setup ogl
		FloatBuffer pos = FloatBuffer.wrap(new float[] { 5.0f, 5.0f, 10.0f, 0.0f});


		GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, pos);
		GL11.glEnable(GL11.GL_CULL_FACE);
		GL11.glEnable(GL11.GL_LIGHTING);
		GL11.glEnable(GL11.GL_LIGHT0);
		GL11.glEnable(GL11.GL_DEPTH_TEST);

		// make ob to initialize all our primitives
		glPrimitives = new Ob();		
		

		GL11.glEnable(GL11.GL_NORMALIZE);

		GL11.glMatrixMode(GL11.GL_PROJECTION);

		System.err.println("GL_VENDOR: " + GL11.glGetString(GL11.GL_VENDOR));
		System.err.println("GL_RENDERER: " + GL11.glGetString(GL11.GL_RENDERER));
		System.err.println("GL_VERSION: " + GL11.glGetString(GL11.GL_VERSION));
		System.err.println();
		System.err.println("glLoadTransposeMatrixfARB() supported: " + GLContext.getCapabilities().GL_ARB_transpose_matrix);
		if (!GLContext.getCapabilities().GL_ARB_transpose_matrix) {
			// --- not using extensions
			GL11.glLoadIdentity();
		} else {
			// --- using extensions
			final FloatBuffer identityTranspose = BufferUtils.createFloatBuffer(16).put(																																									
				new float[] 
				  { 1, 0, 0, 0, 0, 1, 0, 0,
					0, 0, 1, 0, 0, 0, 0, 1});
			identityTranspose.flip();
			ARBTransposeMatrix.glLoadTransposeMatrixARB(identityTranspose);
		}

		float h = (float) screenHeight / (float) screenWidth;
		GL11.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glLoadIdentity();
//		GL11.glTranslatef(0.0f, 0.0f, -40.0f);
		
		//Controls
		Controller.createKeyboard();
	}

}
